# -*- coding: utf-8 -*-
from apps import app
import os
from flask import render_template,request,make_response
from werkzeug.utils import secure_filename
import uuid


UPLOAD_PATH = os.path.join(os.path.dirname(__file__),'images')
@app.route('/')
@app.route('/index/')
def index():
    return render_template('upload.html')


@app.route('/upload/',methods=['GET','POST'])
def upload():
    if request.method =='GET':
        return render_template('upload.html')
    else:
        desc = request.form.get('desc')
        avatar = request.files.get('avatar')
        filename = str(uuid.uuid4()) + "." +secure_filename(avatar.filename)
        print(filename)
        avatar.save(os.path.join(UPLOAD_PATH,filename))
        print(desc)
        img = open(os.path.join(UPLOAD_PATH,filename),'rb').read()
        response = make_response(img)
        response.headers['Content-Type'] = 'image/png'
        return response


